package mn.more.wits.client;

import com.google.gwt.user.client.ui.*;
import mn.more.wits.client.dto.AuthDTO;
import mn.more.wits.client.gui.WitsButton;
import mn.more.wits.client.util.ClientModelUtil;
import mn.more.wits.client.util.InputValidator;
import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.Registry;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.Component;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.KeyPressTextBox;

/**
 * @author <a href="mailto:mike.liu@aptechmongolia.edu.mn">Mike Liu</a>
 * @version $Id: LoginPanel.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public class LoginPanel extends ContentPanel {

	public static final String ID = "login-panel";

	private static final AppMsg APP_MSG = AppMsg.Self.get();
	private static final HasHorizontalAlignment.HorizontalAlignmentConstant ALIGN_RIGHT =
		HasHorizontalAlignment.ALIGN_RIGHT;

	private DisclosurePanel pnInstruction = new DisclosurePanel(APP_MSG.loginInstructionTitle(), false);
	private Label lblInstruction = new Label(APP_MSG.loginInstructionText());
	private Label lblUserName = new Label(APP_MSG.username(), false);
	private Label lblPassword = new Label(APP_MSG.password(), false);
	private KeyPressTextBox tbUserName = new KeyPressTextBox();
	private PasswordTextBox tbPassword = new PasswordTextBox();

	private FlexTable tableLogin = new FlexTable();
	private AuthCallback loginCallback = new AuthCallback();
	private Button btnLogin = new WitsButton(APP_MSG.login(), new LoginClickListener());
	private Button btnReset = new WitsButton(APP_MSG.reset(), new ResetClickListener());

	private class LoginClickListener implements SelectionListener {

		public void widgetSelected(BaseEvent be) {
			String username = tbUserName.getText();
			String password = tbPassword.getText();

			String title = APP_MSG.errorTitle();

			boolean proceed = true;
			if (username == null || username.length() < 1) {
				proceed = false;
				Info.show(title, APP_MSG.required(APP_MSG.fieldUsername()), "");
			} else {
				if (!InputValidator.isValidUsername(username)) {
					proceed = false;
					Info.show(title, APP_MSG.notValid(APP_MSG.fieldUsername()), "");
				}
			}

			if (password == null || password.length() < 1) {
				proceed = false;
				Info.show(title, APP_MSG.required(APP_MSG.fieldPassword()), "");
			} else {
				if (!InputValidator.isValidPassword(password)) {
					proceed = false;
					Info.show(title, APP_MSG.notValid(APP_MSG.fieldPassword()), "");
				}
			}

			if (proceed) {
				AppContext.showLoading();
				AuthDTO auth = new AuthDTO();
				auth.setUsername(username);
				auth.setPassword(password);
				AuthProvider.App.getInstance().authenticate(ClientModelUtil.toPayload(auth), loginCallback);
			}
		}

	}

	private class ResetClickListener implements SelectionListener {

		public void widgetSelected(BaseEvent be) { resetForm(); }
	}

	public LoginPanel() {

		super(Style.HEADER);

		setId(ID);
		setStyleName("pn-login");
		setPadding(10);
		setScrollEnabled(true);
		setSize(350, 250);
		setText(APP_MSG.loginTitle());
		setFrame(true);

		initInstruction();
		initLoginForm();

		add(pnInstruction, DockPanel.NORTH);
		add(tableLogin, DockPanel.CENTER);

		Registry.register(ID, this);
	}

	public void resetForm() {
		tbUserName.setText("");
		tbPassword.setText("");
		tbUserName.setFocus(true);
	}

	public Component focus() {
		this.tbUserName.setFocus(true);
		return this;
	}

	public void setFocus(boolean focus) { this.tbUserName.setFocus(focus); }

	private void initLoginForm() {
		lblUserName.setStyleName(AppContext.DEFAULT_STYLE_LABEL);
		lblUserName.addStyleName("lbl-username");

		tbUserName.addKeyboardListener(new KeyboardListenerAdapter() {
			public void onKeyPress(Widget widget, char c, int i) {
				if (c == 13) {
					tbUserName.setFocus(false);
					tbPassword.setFocus(true);
				} else {
					super.onKeyPress(widget, c, i);
				}
			}
		});
		tbUserName.setStyleName("tb-username");
		tbUserName.setTitle(APP_MSG.tooltipUsername());

		lblPassword.setStyleName(AppContext.DEFAULT_STYLE_LABEL);
		lblPassword.addStyleName("lbl-password");

		tbPassword.addKeyboardListener(new KeyboardListenerAdapter() {
			public void onKeyPress(Widget widget, char c, int i) {
				if (c == 13) {
					btnLogin.fireEvent(Events.Select);
				} else {
					super.onKeyPress(widget, c, i);
				}
			}
		});
		tbPassword.setStyleName("tb-password");
		tbPassword.setTitle(APP_MSG.tooltipPassword());

		btnLogin.setToolTip(APP_MSG.tooltipLogin());
		btnLogin.setIconStyle("login-button-icon");

		btnReset.setToolTip(APP_MSG.tooltipReset());
		btnReset.setIconStyle("reset-button-icon");

		tableLogin.setWidget(0, 0, lblUserName);
		tableLogin.setWidget(1, 0, lblPassword);
		tableLogin.setWidget(0, 1, tbUserName);
		tableLogin.setWidget(1, 1, tbPassword);
		tableLogin.setWidget(2, 1, btnLogin);
		tableLogin.setWidget(2, 2, btnReset);

		FlexTable.FlexCellFormatter format = tableLogin.getFlexCellFormatter();
		format.setColSpan(0, 1, 2);
		format.setColSpan(1, 1, 2);
		format.setHorizontalAlignment(2, 1, ALIGN_RIGHT);
		format.setHorizontalAlignment(2, 2, ALIGN_RIGHT);
		//format.setWidth(2, 2, "30px");

		tbUserName.setTabIndex(1);
		tbPassword.setTabIndex(2);
		btnLogin.setTabIndex(3);
		btnReset.setTabIndex(4);
	}

	private void initInstruction() {
		lblInstruction.setStyleName("lbl-login-instruction");
		pnInstruction.setStyleName("pn-login-instruction");
		pnInstruction.setContent(lblInstruction);
	}
}